package com.lnsoft.cac.server.handle.codec.frameMessage;


import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.Data;
import lombok.NonNull;

@Data
/**
 * 事件信息上送
 * */
public class EventUpload_Cfm {
    private int Measure_Point_ID;                   //1byte         监测点号
    private int Command_Status;                     //1byte         数据发送状态：①0xFF成功②0x00失败


    public ByteBuf toByteBuf() throws Exception{
        if (!Util.measurePointIDIsAvailable(Measure_Point_ID)) {
            throw new Exception("监测点号 设置异常");
        }
        if (Command_Status != (byte)0xFF && Command_Status != (byte)0x00) {
            throw new Exception("数据发送状态 设置异常");
        }
        ByteBuf bf = Unpooled.buffer(2);
        bf.writeByte(Measure_Point_ID);
        bf.writeByte(Command_Status);
        return bf;
    }


    //  解析 [事件信息上送 - 确认] 帧报文内容区 到消息体中
    public static EventUpload_Cfm decFrameBody(@NonNull byte[] frameBody) throws Exception {
        ByteBuf bf = Unpooled.wrappedBuffer(frameBody);
        EventUpload_Cfm msg = new EventUpload_Cfm();
        int len = frameBody.length;
        if (len == 0) {
            return null;
        }
        if (len != 2) {
            throw new Exception("frameBody 数据异常");
        }
        msg.setMeasure_Point_ID(bf.getByte(0) & 0xFF);
        msg.setCommand_Status(bf.getByte(1) & 0xFF);
        return msg;
    }

}
